package com.avicsafety.ezsystem.android_networking;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONArrayRequestListener;
import com.avicsafety.ezsystem.R;
import com.avicsafety.ezsystem.entity.User;
import com.avicsafety.ezsystem.entity.cate;
import com.avicsafety.ezsystem.entity.goods;
import com.avicsafety.ezsystem.search.MyService;
//import com.rx2androidnetworking.Rx2AndroidNetworking;

import org.json.JSONArray;
import org.json.JSONObject;
import org.reactivestreams.Publisher;

import java.io.File;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class NetworkingActivity extends AppCompatActivity {

    private static final String TAG = "NetworkingActivity";
    private Button btn_get;
    private Button btn_get2;

    private Context oThis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_networking);

        oThis = this;

        btn_get = findViewById(R.id.btn_get);
        btn_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                test();
            }
        });

        btn_get2 = findViewById(R.id.btn_get2);
        btn_get2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getLinkRequest();
            }
        });



    }

    private void getSimpleRequest() {
//        Rx2AndroidNetworking.get("https://ps.haishengshop.com/public/goods_cate/tree")
//            .addPathParameter("name","shi")
//            .build()
//            .getObjectListObservable(cate.class)  //可以选择非集合对象
//            .subscribeOn(Schedulers.io()) //访问网络的进程
//            .observeOn(AndroidSchedulers.mainThread()) //切换到主线程
//            .subscribe(new Consumer<List<cate>>() {
//                @Override
//                public void accept(@NonNull List<cate> data) {
//                    Log.e(TAG, "accept: success ：" + data.size());
//                }
//            }, new Consumer<Throwable>() {
//                @Override
//                public void accept(@NonNull Throwable throwable) {
//                    Log.e(TAG, "accept: error :" + throwable.getMessage());
//                }
//            });

        AndroidNetworking.get("https://ps.haishengshop.com/public/goods_cate/tree")
                //.addPathParameter("pageNumber", "0")
                //.addQueryParameter("limit", "3")
                //.addHeaders("token", "1234")
                .setTag("test")
                .setPriority(Priority.LOW)
                .build()
                .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                        // do anything with response
                        Log.v("================",response.toString());
                    }
                    @Override
                    public void onError(ANError error) {
                        // handle error
                    }
                });
    }

    Observer<String> observer = new Observer<String>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(String file) {
            Log.e("",file);
        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    };



    public void test(){

//       Rx2AndroidNetworking.get("http://www.avicsafety.com/ip_search.aspx")
//                .build()
//                .getObjectObservable(JSONObject.class)  //可以选择非集合对象
//                .subscribeOn(Schedulers.io()) //访问网络的进程
//                .observeOn(AndroidSchedulers.mainThread()) //切换到主线程
//                .subscribe(new Consumer<JSONObject>() {
//                    @Override
//                    public void accept(@NonNull JSONObject obj) {
//                        Log.e(TAG, "accept: success ：" + obj.toString());
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(@NonNull Throwable throwable) {
//                        Log.e(TAG, "accept: error :" + throwable.getMessage());
//                    }
//                });


//        Observable
//            .just("464", "458", "453", "450").
//                flatMap(new Function<String, Observable<String>>() {
//                    @Override
//                    public Observable<String> apply(String s) throws Exception {
//                        return getText(s);
//                    }
//                })
//                .subscribeOn(Schedulers.io())
//            .subscribe(observer);
    }

    //我写了一个新闻信息
//    private Observable<String> getText(String id) {
//        Log.e("",id);
//        return Rx2AndroidNetworking.get("http://www.avicsafety.com/article.aspx")
//                .addPathParameter("id",id)
//                .build()
//                .getObjectObservable(String.class);
//    }


    ///链式调用==========================
    //我写了一个获取分类列表的
//    private Observable<List<cate>> getCateList() {
//        return Rx2AndroidNetworking.get("https://ps.haishengshop.com/public/goods_cate/tree")
//                .addPathParameter("name","shi")
//                .build()
//                .getObjectListObservable(cate.class);
//    }

    //我又写了一个商品信息
//    private Observable<List<goods>> getGoods(Long id) {
//        return Rx2AndroidNetworking.get("https://ps.haishengshop.com/public/goods_cate/tree")
//                .addPathParameter("name","shi")
//                .build()
//                .getObjectListObservable(goods.class);
//    }

    //一次调用上边两个请求
    public void start() {

//        getCateList()   //先获取这个
//                .flatMap(new Function<List<cate>, ObservableSource<cate>>() { // flatMap - to return users one by one
//                    @Override
//                    public ObservableSource<cate> apply(List<cate> cateList) {
//                        return Observable.fromIterable(cateList);
//                    }
//                }).flatMap(new Function<cate, ObservableSource<List<goods>>() {
//                    @Override
//                    public ObservableSource<List<goods>> apply(cate cate) throws Exception {
//                        return getGoods(cate.getId());
//                    }
//                })
//                .subscribeOn(Schedulers.newThread())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<List<goods>>() {
//                    @Override
//                    public void accept(List<goods> goods) throws Exception {
//                        Log.v("",""+goods.size());
//                    }
//                });
    }


    private void getLinkRequest() {
//
//        MyService service = new MyService();
//        service.action();
//


    }
}
